<template>
  <div id="shopclassify">  

      <div  style="display:flex;align-items: center;">
            <img class="top_img" src="@/assets/images/a8.png" style="width:35px;height:35px;margin-right:10px;"/>
            <div style="line-height: 40px;font-weight:bold:">商品分类</div>
      </div> 
   
      <el-form ref="form"  label-width="80px">  
            <el-row>   
                <el-col :span="24" style="text-align: right;">
                   <el-button class="custom_color_button1" type="primary"  style="margin-bottom:10px;" @click="add_but()">
                     <img class="custom_color_button1_img" src="@/assets/images/but_bg2.png" />添加</el-button>
                </el-col>           
            </el-row>
   </el-form>

   <!-- 渲染数据list -->
   <shopclassify-list :datalist="datalist" @up_list_fun="list_fun"> </shopclassify-list>
 
    <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>



        <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="add_mtk"  width="500px" :close-on-click-modal="false">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="70px" style="width:100%;">
    
             <el-form-item label="名称">
                    <el-input    v-model="mtk_form.cname" placeholder="名称"></el-input>
              </el-form-item> 
 

               <el-row style="width:100%;">
                     <el-col :span="12">
                        <el-form-item label="图片">
                            <el-upload 
                                  action="uploadUrl"  
                                  :show-file-list="false"   
                                  :before-upload="beforeupload"
                                  :http-request="ImgUploadSectionFile">
                                  <div class="avatar-uploader">
                                      <img v-if="pic_form.head_img"  :src="pic_form.head_img" class="avatar">
                                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                  </div>
                            </el-upload>
                         </el-form-item>       
                     </el-col> 

                </el-row> 
                
               <el-form-item label="颜色选择">
                 <el-color-picker v-model="mtk_form.color" size="mini"></el-color-picker>
               </el-form-item> 
 
                                     
            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;"  @click="add_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>

      
  </div>
</template>

<script>
import ShopclassifyList from '@/view/Shop/ShopclassifyList'
export default {
  name: 'shopclassify',
  data () {
    return {
        datalist:[],
        page:1,
        count:0,
        add_mtk:false,
        mtk_form:{
           cname:'',
           color: '#778899'
        },
        pic_form:{
           head_img:'',
           upfile:'',
        }, 
    }
  },
 components:{
    ShopclassifyList
  },
  created(){
     this.list_fun()
  },
  mounted (){
    
  },
  methods: {
      list_fun(){
          this.post("/product/listOfCate", {

	        }).then(res => {
            this.datalist = res.result
            console.log(res)
         })
      },
      add_but(){
          this.add_mtk = true
      },
      add_but_qr(){
        this.post("/product/addCate", {
                cname:this.mtk_form.cname,
                icon:this.pic_form.upfile,
                color:this.mtk_form.color,
              }).then(res => {
                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.list_fun()
                    this.add_mtk = false
                }else {
                   this.open1(res.message,'warning')
                }

            })
      },
      beforeupload(file){   //重新写入上穿 需要点击确认上传与表单一起提交
           if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }  
            var windowURL = window.URL || window.webkitURL;               
            this.pic_form.head_img=windowURL.createObjectURL(file);             
            
            this.upimgpost("/image/uploadMultiple",file).then(res => {
              console.log(res.result.url )
               this.pic_form.upfile = res.result.url    
            })

            return false;
     },
      ImgUploadSectionFile(param){//图片上传    
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
     

  }
}
</script>


<style scoped>
.avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>
